package com.example.mystoryapp2.data.network.model

import android.util.Log
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.service.StoryService
import com.example.mystoryapp2.domain.Category
import com.example.mystoryapp2.domain.Story
import retrofit2.Call
import retrofit2.Response

object StoryModel {
    private val TAG = "StoryModel"
    private var storyService = ServiceCreator.create<StoryService>()
    var categoryList = ArrayList<Category>()

    init {
        Log.d(TAG, ": StoryModel执行了")
        getCategoryList()
    }

    fun getDefaultCategory():Category{
        val category = Category()
        category.setCategoryId(-1)
        category.setCategoryName(AppContext.application
            .baseContext.getString(R.string.category_all))
        return category
    }

    fun getCategoryById(id:Int):Category{
        for (category in categoryList) {
            if(category.getCategoryId() == id){
                return category
            }
        }
        return Category.getUnKnow()
    }

    fun buildCategoryStr(id:Int?):String {
        if(id==null){
            return Category.getUnKnow().getCategoryName()
        }
        return getCategoryById(id).getCategoryName()
    }

    fun getLocationById(id:Int?):Int{
        if(id==null){
            return 0;
        }
        for ((index, category) in categoryList.withIndex()) {
            if(category.getCategoryId()==id){
                return index
            }
        }
        return 0
    }

    /**
     * 根据storyId获取Story
     */
    fun getStoryById(storyId: Int, callBack: ServiceCreator.ICallBack<Story>) {
        storyService.getStoryById(storyId)
            .enqueue(object : NetCallBack<Story>() {
                override fun onResponse(call: Call<Story>, response: Response<Story>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<Story>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

    /**
     * 获取未发布的故事
     */
    fun getPublishedStory(
        userId: Int, callBack: ServiceCreator.ICallBack<List<Story>>,
        published: Boolean = false
    ) {
        if (published) {
            return storyService.getPublishedStory(userId)
                .enqueue(object : NetCallBack<List<Story>>() {
                    override fun onResponse(
                        call: Call<List<Story>>, response: Response<List<Story>>
                    ) {
                        val body = response.body()
                        if (body != null) {
                            callBack.onSuccess(body)
                        }
                    }
                })
        } else {
            return storyService.getUnpublishedStory(userId)
                .enqueue(object : NetCallBack<List<Story>>() {
                    override fun onResponse(
                        call: Call<List<Story>>, response: Response<List<Story>>
                    ) {
                        val body = response.body()
                        if (body != null) {
                            callBack.onSuccess(body)
                        }
                    }
                })
        }
    }

    /**
     * 添加story
     */
    fun addStory(story: Story, callBack: ServiceCreator.ICallBack<Boolean>) {
        return storyService.addStory(story).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                if (response.body() == true) {
                    callBack.onSuccess(true)
                }
            }
        })
    }

    /**
     * 添加story
     */
    fun updateStory(story: Story, callBack: ServiceCreator.ICallBack<Boolean>) {
        return storyService.updateStory(story).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                if (response.body() == true) {
                    callBack.onSuccess(true)
                }
            }
        })
    }

    /**
     * 添加story
     */
    fun hasPage(storyId: Int, callBack: ServiceCreator.ICallBack<Boolean>) {
        return storyService.hasPage(storyId).enqueue(object : NetCallBack<Boolean>() {
            override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                if (response.body() == true) {
                    callBack.onSuccess(true)
                }else{
                    callBack.onSuccess(false)
                }
            }

            override fun onFailure(call: Call<Boolean>, t: Throwable) {
                super.onFailure(call, t)
                callBack.onSuccess(false)

            }
        })
    }


    /**
     * 可以改成异步
     */
    private fun getCategoryList() {
        return storyService.getCategoryList().enqueue(object : NetCallBack<List<Category>>() {
            override fun onResponse(
                call: Call<List<Category>>,
                response: Response<List<Category>>
            ) {
                if(response.body()!=null){
                    categoryList = response.body() as ArrayList<Category>
                }
            }
        })
    }

    fun getCollectedStory(storyId: Int, callBack: ServiceCreator.ICallBack<List<Story>>) {
        storyService.getCollectedStory(storyId)
            .enqueue(object : NetCallBack<List<Story>>() {
                override fun onResponse(call: Call<List<Story>>, response: Response<List<Story>>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }
                override fun onFailure(call: Call<List<Story>>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

    fun search(story: Story, callBack: ServiceCreator.ICallBack<List<Story>>) {
        storyService.search(story)
            .enqueue(object : NetCallBack<List<Story>>() {
                override fun onResponse(call: Call<List<Story>>, response: Response<List<Story>>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }
                override fun onFailure(call: Call<List<Story>>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

}